home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.1.5 / components / nsFilePicker.js < prev    next >
Text File  |  2009-11-09  |  12KB  |  351 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 2000
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Stuart Parmenter <pavlov@netscape.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  27.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /*
  40.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  41.  * If you must perform some initialization, and it could possibly fail (even
  42.  * due to an out-of-memory condition), you should use an Init method, which
  43.  * can convey failure appropriately (thrown exception in JS,
  44.  * NS_FAILED(nsresult) return in C++).
  45.  *
  46.  * In JS, you can actually cheat, because a thrown exception will cause the
  47.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  48.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  49.  * for portability reasons -- and even when you're building completely
  50.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  51.  */
  52.  
  53.  
  54. const DEBUG = false; /* set to true to enable debug messages */
  55.  
  56. const FILEPICKER_CONTRACTID     = "@mozilla.org/filepicker;1";
  57. const FILEPICKER_CID        = Components.ID("{54ae32f8-1dd2-11b2-a209-df7c505370f8}");
  58. const LOCAL_FILE_CONTRACTID = "@mozilla.org/file/local;1";
  59. const APPSHELL_SERV_CONTRACTID  = "@mozilla.org/appshell/appShellService;1";
  60. const STRBUNDLE_SERV_CONTRACTID = "@mozilla.org/intl/stringbundle;1";
  61.  
  62. const nsIAppShellService    = Components.interfaces.nsIAppShellService;
  63. const nsILocalFile          = Components.interfaces.nsILocalFile;
  64. const nsIFileURL            = Components.interfaces.nsIFileURL;
  65. const nsISupports           = Components.interfaces.nsISupports;
  66. const nsIFactory            = Components.interfaces.nsIFactory;
  67. const nsIFilePicker         = Components.interfaces.nsIFilePicker;
  68. const nsIInterfaceRequestor = Components.interfaces.nsIInterfaceRequestor
  69. const nsIDOMWindow          = Components.interfaces.nsIDOMWindow;
  70. const nsIStringBundleService = Components.interfaces.nsIStringBundleService;
  71. const nsIWebNavigation      = Components.interfaces.nsIWebNavigation;
  72. const nsIDocShellTreeItem   = Components.interfaces.nsIDocShellTreeItem;
  73. const nsIBaseWindow         = Components.interfaces.nsIBaseWindow;
  74.  
  75. var   bundle                = null;
  76. var   lastDirectory         = null;
  77.  
  78. function nsFilePicker()
  79. {
  80.   if (!bundle)
  81.     bundle = srGetStrBundle("chrome://global/locale/filepicker.properties");
  82.  
  83.   /* attributes */
  84.   this.mDefaultString = "";
  85.   this.mFilterIndex = 0;
  86.   this.mFilterTitles = new Array();
  87.   this.mFilters = new Array();
  88.   this.mDisplayDirectory = null;
  89.   if (lastDirectory) {
  90.     try {
  91.       var dir = Components.classes[LOCAL_FILE_CONTRACTID].createInstance(nsILocalFile);
  92.       dir.initWithPath(lastDirectory);
  93.       this.mDisplayDirectory = dir;
  94.     } catch (e) {}
  95.   }
  96. }
  97.  
  98. nsFilePicker.prototype = {
  99.  
  100.   /* attribute nsILocalFile displayDirectory; */
  101.   set displayDirectory(a) {
  102.     this.mDisplayDirectory = a &&
  103.       a.clone().QueryInterface(nsILocalFile);
  104.   },
  105.   get displayDirectory()  {
  106.     return this.mDisplayDirectory &&
  107.            this.mDisplayDirectory.clone()
  108.                .QueryInterface(nsILocalFile);
  109.   },
  110.  
  111.   /* readonly attribute nsILocalFile file; */
  112.   set file(a) { throw "readonly property"; },
  113.   get file()  { return this.mFilesEnumerator.mFiles[0]; },
  114.  
  115.   /* readonly attribute nsISimpleEnumerator files; */
  116.   set files(a) { throw "readonly property"; },
  117.   get files()  { return this.mFilesEnumerator; },
  118.  
  119.   /* readonly attribute nsIURI fileURL; */
  120.   set fileURL(a) { throw "readonly property"; },
  121.   get fileURL()  {
  122.     if (this.mFileURL)
  123.       return this.mFileURL;
  124.  
  125.     if (!this.mFilesEnumerator)
  126.       return null;
  127.  
  128.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  129.                     .getService(Components.interfaces.nsIIOService);
  130.  
  131.     return this.mFileURL = ioService.newFileURI(this.file);
  132.   },
  133.  
  134.   /* attribute wstring defaultString; */
  135.   set defaultString(a) { this.mDefaultString = a; },
  136.   get defaultString()  { return this.mDefaultString; },
  137.  
  138.   /* attribute wstring defaultExtension */
  139.   set defaultExtension(ext) { },
  140.   get defaultExtension() { return ""; },
  141.  
  142.   /* attribute long filterIndex; */
  143.   set filterIndex(a) { this.mFilterIndex = a; },
  144.   get filterIndex() { return this.mFilterIndex; },
  145.  
  146.   /* members */
  147.   mFilesEnumerator: undefined,
  148.   mParentWindow: null,
  149.  
  150.   /* methods */
  151.   init: function(parent, title, mode) {
  152.     this.mParentWindow = parent;
  153.     this.mTitle = title;
  154.     this.mMode = mode;
  155.   },
  156.  
  157.   appendFilters: function(filterMask) {
  158.     if (filterMask & nsIFilePicker.filterHTML) {
  159.       this.appendFilter(bundle.GetStringFromName("htmlTitle"),
  160.                    bundle.GetStringFromName("htmlFilter"));
  161.     }
  162.     if (filterMask & nsIFilePicker.filterText) {
  163.       this.appendFilter(bundle.GetStringFromName("textTitle"),
  164.                    bundle.GetStringFromName("textFilter"));
  165.     }
  166.     if (filterMask & nsIFilePicker.filterImages) {
  167.       this.appendFilter(bundle.GetStringFromName("imageTitle"),
  168.                    bundle.GetStringFromName("imageFilter"));
  169.     }
  170.     if (filterMask & nsIFilePicker.filterXML) {
  171.       this.appendFilter(bundle.GetStringFromName("xmlTitle"),
  172.                    bundle.GetStringFromName("xmlFilter"));
  173.     }
  174.     if (filterMask & nsIFilePicker.filterXUL) {
  175.       this.appendFilter(bundle.GetStringFromName("xulTitle"),
  176.                    bundle.GetStringFromName("xulFilter"));
  177.     }
  178.     this.mAllowURLs = !!(filterMask & nsIFilePicker.filterAllowURLs);
  179.     if (filterMask & nsIFilePicker.filterApps) {
  180.       // We use "..apps" as a special filter for executable files
  181.       this.appendFilter(bundle.GetStringFromName("appsTitle"),
  182.                         "..apps");
  183.     }
  184.     if (filterMask & nsIFilePicker.filterAll) {
  185.       this.appendFilter(bundle.GetStringFromName("allTitle"),
  186.                    bundle.GetStringFromName("allFilter"));
  187.     }
  188.   },
  189.  
  190.   appendFilter: function(title, extensions) {
  191.     this.mFilterTitles.push(title);
  192.     this.mFilters.push(extensions);
  193.   },
  194.  
  195.   QueryInterface: function(iid) {
  196.     if (iid.equals(nsIFilePicker) ||
  197.         iid.equals(nsISupports))
  198.       return this;
  199.  
  200.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  201.     return null;
  202.   },
  203.  
  204.   show: function() {
  205.     var o = new Object();
  206.     o.title = this.mTitle;
  207.     o.mode = this.mMode;
  208.     o.displayDirectory = this.mDisplayDirectory;
  209.     o.defaultString = this.mDefaultString;
  210.     o.filterIndex = this.mFilterIndex;
  211.     o.filters = new Object();
  212.     o.filters.titles = this.mFilterTitles;
  213.     o.filters.types = this.mFilters;
  214.     o.allowURLs = this.mAllowURLs;
  215.     o.retvals = new Object();
  216.  
  217.     var parent;
  218.     if (this.mParentWindow) {
  219.       parent = this.mParentWindow;
  220.     } else if (typeof(window) == "object" && window != null) {
  221.       parent = window;
  222.     } else {
  223.       try {
  224.         var appShellService = Components.classes[APPSHELL_SERV_CONTRACTID].getService(nsIAppShellService);
  225.         parent = appShellService.hiddenDOMWindow;
  226.       } catch(ex) {
  227.         debug("Can't get parent.  xpconnect hates me so we can't get one from the appShellService.\n");
  228.         debug(ex + "\n");
  229.       }
  230.     }
  231.  
  232.     var parentWin = null;
  233.     try {
  234.       parentWin = parent.QueryInterface(nsIInterfaceRequestor)
  235.                         .getInterface(nsIWebNavigation)
  236.                         .QueryInterface(nsIDocShellTreeItem)
  237.                         .treeOwner
  238.                         .QueryInterface(nsIInterfaceRequestor)
  239.                         .getInterface(nsIBaseWindow);
  240.     } catch(ex) {
  241.       dump("file picker couldn't get base window\n"+ex+"\n");
  242.     }
  243.     try {
  244.       if (parentWin)
  245.         parentWin.blurSuppression = true;
  246.       parent.openDialog("chrome://global/content/filepicker.xul",
  247.                         "",
  248.                         "chrome,modal,titlebar,resizable=yes,dependent=yes",
  249.                         o);
  250.       if (parentWin)
  251.         parentWin.blurSuppression = false;
  252.  
  253.       this.mFilterIndex = o.retvals.filterIndex;
  254.       this.mFilesEnumerator = o.retvals.files;
  255.       this.mFileURL = o.retvals.fileURL;
  256.       lastDirectory = o.retvals.directory;
  257.       return o.retvals.buttonStatus;
  258.     } catch(ex) { dump("unable to open file picker\n" + ex + "\n"); }
  259.  
  260.     return null;
  261.   }
  262. }
  263.  
  264. if (DEBUG)
  265.     debug = function (s) { dump("-*- filepicker: " + s + "\n"); }
  266. else
  267.     debug = function (s) {}
  268.  
  269. /* module foo */
  270.  
  271. var filePickerModule = new Object();
  272.  
  273. filePickerModule.registerSelf =
  274. function (compMgr, fileSpec, location, type)
  275. {
  276.     debug("registering (all right -- a JavaScript module!)");
  277.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  278.  
  279.     compMgr.registerFactoryLocation(FILEPICKER_CID,
  280.                                     "FilePicker JS Component",
  281. //@line 284 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/toolkit/components/filepicker/src/nsFilePicker.js.in"
  282.                                     "",
  283. //@line 286 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/toolkit/components/filepicker/src/nsFilePicker.js.in"
  284.                                     fileSpec,
  285.                                     location,
  286.                                     type);
  287. }
  288.  
  289. filePickerModule.getClassObject =
  290. function (compMgr, cid, iid) {
  291.     if (!cid.equals(FILEPICKER_CID))
  292.         throw Components.results.NS_ERROR_NO_INTERFACE;
  293.  
  294.     if (!iid.equals(Components.interfaces.nsIFactory))
  295.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  296.  
  297.     return filePickerFactory;
  298. }
  299.  
  300. filePickerModule.canUnload =
  301. function(compMgr)
  302. {
  303.     debug("Unloading component.");
  304.     return true;
  305. }
  306.  
  307. /* factory object */
  308. var filePickerFactory = new Object();
  309.  
  310. filePickerFactory.createInstance =
  311. function (outer, iid) {
  312.     debug("CI: " + iid);
  313.     debug("IID:" + nsIFilePicker);
  314.     if (outer != null)
  315.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  316.  
  317.     return (new nsFilePicker()).QueryInterface(iid);
  318. }
  319.  
  320. /* entrypoint */
  321. function NSGetModule(compMgr, fileSpec) {
  322.     return filePickerModule;
  323. }
  324.  
  325.  
  326.  
  327. /* crap from strres.js that I want to use for string bundles since I can't include another .js file.... */
  328.  
  329. var strBundleService = null;
  330.  
  331. function srGetStrBundle(path)
  332. {
  333.   var strBundle = null;
  334.  
  335.   if (!strBundleService) {
  336.     try {
  337.       strBundleService = Components.classes[STRBUNDLE_SERV_CONTRACTID].getService(nsIStringBundleService);
  338.     } catch (ex) {
  339.       dump("\n--** strBundleService createInstance failed **--\n");
  340.       return null;
  341.     }
  342.   }
  343.  
  344.   strBundle = strBundleService.createBundle(path);
  345.   if (!strBundle) {
  346.     dump("\n--** strBundle createInstance failed **--\n");
  347.   }
  348.   return strBundle;
  349. }
  350.  
  351.